package roslan.musicbox.preprocess;

import java.io.File;
import java.io.PrintWriter;
import java.util.Scanner;

public class SplitXMLDatabase {
	public void split(String inputName, int partNum){
		Scanner scanner = null;
		PrintWriter[] writerArray = new PrintWriter[partNum];
		try{
			scanner = new Scanner(new File(inputName+".xml"), "utf8");
			for(int i = 0; i< partNum; ++i){
				writerArray[i] = new PrintWriter(inputName+"."+i+".xml","utf8");
				writerArray[i].println(
					"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>");
				writerArray[i].println("<MusicTagDatabase>");
			}
		}catch(Exception e){
		}
		scanner.nextLine();
		scanner.nextLine();
		int count = 0;
		while(scanner.hasNextLine()){
			String res = scanner.nextLine();
			if(res.equals("</MusicTagDatabase>")){
				break;
			}
			writerArray[count].println(res);
			if(res.equals("</MusicTag>")){
				count = (count+1)%partNum;
			}
		}
		for(PrintWriter writer : writerArray){
			writer.println("</MusicTagDatabase>");
			writer.flush();
			writer.close();
		}
	}
	public static void main(String[] args){
		new SplitXMLDatabase().split("test/tagdb", 10);
	}
}
